package net.chenyan.server.repository;/**
 * @author chenyan
 * @createTime 2022/5/5 14:40
 * @description description
 */

import net.chenyan.server.po.Music;
import net.chenyan.server.po.Mv;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;

/**
 *@description: TODO
 *@author: chenyan
 *@create: 2022/5/5 14:40
 */
@Repository
public interface MusicRepository extends PagingAndSortingRepository<Music,Integer>, JpaSpecificationExecutor<Music> {
    //模糊查询（根据mv名称模糊查询所有数据）
    @Query(value = "select *  from music ms where ms.name LIKE CONCAT('%',:name,'%')  ", nativeQuery = true)
    //分页表连接
    Page<Music> findByNameLike(Pageable pageable, String name);

    @Query(value = "select *  from music  where singer_id=?1", nativeQuery = true)
    Page<Music> findBySingerName(int singerId, Pageable pageable);

    @Query(value = "select *  from music  order by liked_num desc",nativeQuery = true)
    Page<Music> findByLikedNum(Pageable pageable);

    @Query(value = "select *  from music  order by create_time desc",nativeQuery = true)
    Page<Music> findByCreateTime(Pageable pageable);

    //分页查询（根据用户id分页查询所有用户收藏的数据）
    @Query(value = "select m.*  from user_music um inner join user u on u.id=um.user_id inner join music m on m.id=um.music_id where user_id=?1 and is_collect=1",nativeQuery = true)
    Page<Music> findMusicByUserId(int userId, Pageable pageable);

}
